package com.zhang.passer.controller;

import com.alibaba.fastjson.JSON;
import com.zhang.passer.domain.ImageDo;
import com.zhang.passer.domain.UserDo;
import com.zhang.passer.domain.YouJiDo;
import com.zhang.passer.service.ImageService;
import com.zhang.passer.service.UserService;
import com.zhang.passer.service.YoujiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    ImageService imageService;

    @Autowired
    YoujiService youjiService;


    @RequestMapping("/toLogin")
    public String toUserLogin(){
        return "user/login";
    }


    @RequestMapping("/toRegister")
    public String toUserRegister(){
        return "user/register";
    }

    @RequestMapping("/login")
    public String login(String username, String password, ModelMap modelMap, HttpSession session){
        UserDo userDo = userService.queryDetail(username);
        if (ObjectUtils.isEmpty(userDo)){
            modelMap.addAttribute("msg","用户名不存在");
            return "user/login";
        }
        if (userDo.getPassword().equals(password)){
            //用户状态记录
            session.setAttribute("userInfo",userDo);
            //首页图片集合
            List<ImageDo> banner = imageService.queryBanner5();
            List<ImageDo> jingdian = imageService.queryJingdian7();
            List<ImageDo> meishi = imageService.queryMeishi2();
            List<YouJiDo> youji = youjiService.queryNew7();
            modelMap.addAttribute("bannerImage",banner);
            modelMap.addAttribute("jingImage",jingdian);
            modelMap.addAttribute("meishiImage",meishi);
            modelMap.addAttribute("youjis",youji);
            modelMap.addAttribute("userInfo",userDo);
            return "index";
        }else {
            //密码错误
            modelMap.addAttribute("msg","密码输入错误");
            return "user/login";
        }
    }


    @RequestMapping("/register")
    public String register(String username, String password,String password_again,ModelMap modelMap, HttpSession session){
        if (!password.equals(password_again)){
            modelMap.addAttribute("msg","两次输入密码不一致");
            return "user/register";
        }
        if (ObjectUtils.isEmpty(userService.queryDetail(username))){
            //用户注册
            UserDo userDo=new UserDo().setUsername(username).setNickname(username).setPassword(password);
            userService.addUser(userDo);
            //用户状态记录
            session.setAttribute("userInfo",userDo);
            //跳转页面
            List<ImageDo> banner = imageService.queryBanner5();
            List<ImageDo> jingdian = imageService.queryJingdian7();
            List<ImageDo> meishi = imageService.queryMeishi2();
            List<YouJiDo> youji = youjiService.queryNew7();
            modelMap.addAttribute("bannerImage",banner);
            modelMap.addAttribute("jingImage",jingdian);
            modelMap.addAttribute("meishiImage",meishi);
            modelMap.addAttribute("youjis",youji);
            modelMap.addAttribute("userInfo",userDo);
            return "index";
        }else {
            modelMap.addAttribute("msg","用户名已存在");
            return "user/register";
        }

    }

    /**
     * 从Session中获取用户数据
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public String getUserInfoFromSession(HttpSession httpSession){

        UserDo userInfo=null;
        try{
            userInfo= (UserDo) httpSession.getAttribute("userInfo");
        }catch (Exception e){
            // TODO: 2022/1/5 用户未登录记录IP信息到数据库
            System.out.println("未登录");
        }

        return JSON.toJSONString(userInfo);
    }


    /**
     * 注销登录
     * 从Session中销毁用户数据,并返回首页
     */
    @RequestMapping("/logout")
    public String delUserInfoFromSession(HttpSession httpSession,ModelMap modelMap){
        httpSession.removeAttribute("userInfo");
        httpSession.invalidate();

        //跳转首页页面
        List<ImageDo> banner = imageService.queryBanner5();
        List<ImageDo> jingdian = imageService.queryJingdian7();
        List<ImageDo> meishi = imageService.queryMeishi2();
        List<YouJiDo> youji = youjiService.queryNew7();
        modelMap.addAttribute("bannerImage",banner);
        modelMap.addAttribute("jingImage",jingdian);
        modelMap.addAttribute("meishiImage",meishi);
        modelMap.addAttribute("youjis",youji);
        // TODO: 2022/1/5 用户未登录记录IP信息到数据库
        UserDo userInfo=null;
        System.out.println("未登录");
        return "index";
    }
}
